package com.dongdongsongcan.app.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.activity.AddressListActivity;
import com.dongdongsongcan.app.activity.FavouriteListActivity;
import com.dongdongsongcan.app.activity.LoginActivity;
import com.dongdongsongcan.app.common.DDMessageDef;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.db.bean.User;
import com.su.dolphin.core.notification.DpMessageCenter;
import com.su.dolphin.core.proxy.ImageLoader;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.fragment.BaseFragment;

public class UserFragment extends BaseFragment implements DpMessageCenter.INotify {

    private ImageView mUser_avatar;
    private TextView mUser_name;
    private TextView mUser_clublever;
    private TextView mUser_score;

    @Override
    protected int onSetContentView() {
        DpMessageCenter.i().register(this, DDMessageDef.N_USER_CHANGE);
        return R.layout.fragment_user;
    }

    @Override
    public void initView() {
        mUser_avatar = (ImageView) getView().findViewById(R.id.user_avatar);
        mUser_name = (TextView) getView().findViewById(R.id.user_name);
        mUser_clublever = (TextView) getView().findViewById(R.id.user_clublever);
        mUser_score = (TextView) getView().findViewById(R.id.user_score);
    }

    @Override
    public void setListener() {
        getView().findViewById(R.id.user_address_mgr).setOnClickListener(this);
        getView().findViewById(R.id.user_favourite).setOnClickListener(this);
        getView().findViewById(R.id.main_user_logout).setOnClickListener(this);
        mUser_avatar.setOnClickListener(this);
    }

    @Override
    public void initData() {
        User user = UserControl.i().getUser();
        if (user != null) {
            ImageLoader.i().loadImage(mUser_avatar, user.getPic());
            mUser_name.setText(user.getUser_name());
            mUser_score.setText(user.getUser_score() + "");
            ImageLoader.i().loadImage(mUser_avatar, user.getPic());
        }

    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.main_user_logout:
                TipsUtil.showConfirmDialog(getActivity(), "请确认", "是否注销?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        UserControl.i().logout();
                        //getActivity().finish();
                       // DpActivityManager.i().finishApp(getActivity());
                        LoginActivity.startFrom(getActivity());
                    }
                });
                break;
            case R.id.user_address_mgr:
                AddressListActivity.startFrom(getActivity(), AddressListActivity.MODE_EDIT);
                break;
            case R.id.user_avatar:
                //LoginActivity.startFrom(getActivity());
                break;
            case R.id.user_favourite:
                FavouriteListActivity.startFrom(getActivity());
                break;
            default:
                break;
        }
    }

    @Override
    public void update(Bundle bundle) {

    }

    @Override
    public void onNotifyInner(DpMessageCenter.MCMessage mcmessage) {
        if (mcmessage.notificationID == DDMessageDef.N_USER_CHANGE) {
            initData();
        }
    }
}
